import { View } from "../../../../mvc/core/View";
import { BaseItem } from "../../common/BaseItem";
import { SkillItem } from "../../skill/SkillItem";
import { LayerManager } from "../../../manager/LayerManager";
import { Language } from "../../../../../extend/language/Language";
import { CopyConfig } from "../../../resource/CopyConfig";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { SceneRange } from "../../../scene/map/SceneRange";
import { Global } from "../../../../../com/gengine/global/Global";
import { ItemData } from "../../../resource/info/ItemData";
import { ObjCreate } from "../../common/ObjCreate";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { MagicTowerInfoTitle } from "../../copy/magicTower/MagicTowerInfoTitle";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { SkillInfo } from "../../../model/SkillInfo";
import { SkillComposeTipsView } from "./SkillComposeTipsView";
import { SkillConfig } from "../../../resource/SkillConfig";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { Vector } from "../../../../../__AS3__/vec/Vector";
//class SkillComposePanel
    
    export  class SkillComposePanel extends View
    {
        constructor()
        {
            super();
            this._moveRec = new Object /* flash.geom.Rectangle */();
            this.allItem = new Vector.<BaseItem>();
            this.materialItem = new Vector.<BaseItem>();
            this._skillItem = new SkillItem();
            this.codeArr = [];
            this.layer = LayerManager.uiLayer;
            this.show();
            this.createChildren();
            this.title = Language.getString(200212);
            return;
        }

        /* internal  */isCanCompose(): Boolean
        {
            var loc1=null;
            var loc2=0;
            var loc3=this.allItem;
            for(loc1 of loc3) 
            {
                if (loc1.itemData) 
                {
                    continue;
                }
                return false;
            }
            return true;
        }

        /* internal  */getComposeIndex(): string
        {
            var loc2=null;
            var loc1="";
            var loc3=0;
            var loc4=this.allItem;
            for(loc2 of loc4) 
            {
                if (!loc2.itemData) 
                {
                    continue;
                }
                loc1 = loc1 + (loc2.itemData.itemCode + "#");
            }
            return loc1.substr(0, (loc1.length - 1));
        }

        /* internal  */autoSelectMaterial(): void
        {
            var loc4=0;
            var loc1=this.allItem[0].itemData ? this.allItem[0].itemData.itemCode : 0;
            var loc2=this.allItem[1].itemData ? this.allItem[1].itemData.itemCode : 0;
            this.codeArr = CopyConfig.instance.getHeroBattleMaterial(String(loc1), String(loc2));
            var loc3=0;
            while (loc3 < this.materialItem.length) 
            {
                if (this.materialItem[loc3].itemData) 
                {
                    if ((loc4 = this.codeArr.indexOf(this.materialItem[loc3].itemData.itemCode)) == -1) 
                    {
                        this.materialItem[loc3].filters = [FilterConst.colorFilter2];
                    }
                    else if (this.materialItem[loc3].itemData.itemCode == loc1 && this.materialItem[loc3].itemData.itemCode == loc2 && this.materialItem[loc3].itemData.itemAmount < 3) 
                    {
                        this.materialItem[loc3].filters = [FilterConst.colorFilter2];
                    }
                    else if ((this.materialItem[loc3].itemData.itemCode == loc1 || this.materialItem[loc3].itemData.itemCode == loc2) && this.materialItem[loc3].itemData.itemAmount < 2) 
                    {
                        this.materialItem[loc3].filters = [FilterConst.colorFilter2];
                    }
                    else 
                    {
                        this.materialItem[loc3].filters = null;
                    }
                }
                ++loc3;
            }
            return;
        }

        protected onClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (!this._skillItem.skillInfo) 
            {
                return;
            }
            this.clearAllItem();
            this._skillItem.skillData = null;
            this._skillItem.toolTipData = null;
            this._composeBtn.enabled = false;
            this.clearFilter();
            Dispatcher.dispatchEvent(new DataEvent(EventName.HeroBattleComposeSkill, this._skillItem.skillInfo.skillId));
            return;
        }

        protected onDelCompose(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as BaseItem;
            if (loc1.itemData) 
            {
                (arg1.currentTarget as BaseItem).itemData = null;
                this._skillItem.skillData = null;
                this._skillItem.toolTipData = null;
                this._composeBtn.enabled = false;
                this.sortComposeList();
                this.autoSelectMaterial();
            }
            return;
        }

        /* internal  */sortComposeList(): void
        {
            var loc1=0;
            while (loc1 < this.allItem.length) 
            {
                if (this.allItem[loc1].itemData == null && loc1 + 1 < this.allItem.length && this.allItem[loc1 + 1] && this.allItem[loc1 + 1].itemData) 
                {
                    this.allItem[loc1].itemData = this.allItem[loc1 + 1].itemData;
                    this.allItem[loc1 + 1].itemData = null;
                }
                ++loc1;
            }
            return;
        }

        /* internal  */onMoveBtnMouseDown(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._moveRec.x = 0;
            this._moveRec.y = 0;
            this._moveRec.width = SceneRange.display.width - 30;
            this._moveRec.height = SceneRange.display.height - 30;
            startDrag(false, this._moveRec);
            Global.stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }

        /* internal  */onStageMouseUpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            stopDrag();
            Global.stage.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }

        /* internal  */clearFilter(): void
        {
            var loc1=0;
            while (loc1 < this.materialItem.length) 
            {
                if (this.materialItem[loc1].itemData) 
                {
                    this.materialItem[loc1].filters = null;
                }
                ++loc1;
            }
            return;
        }

        public updateMaterial(arg1: Array<any>): void
        {
            var loc1=null;
            this.clearMalterialItem();
            var loc2=0;
            while (loc2 < arg1.length) 
            {
                loc1 = new ItemData(arg1[loc2]);
                this.materialItem[loc2].itemData = loc1;
                this.materialItem[loc2].filters = null;
                ++loc2;
            }
            if (!this.isCanCompose()) 
            {
                this.autoSelectMaterial();
            }
            return;
        }

        public set title(arg1: string)
        {
            this._titleSp.title = arg1;
            return;
        }

        public updatePosition(): void
        {
            x = 200;
            y = 288;
            return;
        }

        public clearAllItem(): void
        {
            var loc1=0;
            while (loc1 < this.allItem.length) 
            {
                if (this.allItem[loc1].itemData) 
                {
                    this.allItem[loc1].itemData = null;
                    this.allItem[loc1].filters = null;
                }
                ++loc1;
            }
            return;
        }

        public clearMalterialItem(): void
        {
            var loc1=0;
            while (loc1 < this.materialItem.length) 
            {
                if (this.materialItem[loc1].itemData) 
                {
                    this.materialItem[loc1].itemData = null;
                    this.materialItem[loc1].filters = null;
                }
                ++loc1;
            }
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            if (this._skillTipsPanel && !this._skillTipsPanel.isHide) 
            {
                this._skillTipsPanel.hide();
            }
            return;
        }

        protected createChildren(): void
        {
            var loc2=0;
            var loc3=0;
            var loc4=null;
            var loc5=null;
            ObjCreate.createBg(0, 24, 300, 200, this);
            this._moveSp = new Object /* flash.display.Sprite */();
            this._moveSp.y = 142;
            this._moveSp.mouseEnabled = false;
            this.addChild(this._moveSp);
            this._bg = new Object /* flash.display.Bitmap */(new Object /* flash.display.BitmapData */(298, 82, false, 0));
            this._bg.x = 1;
            this._bg.alpha = 0.45;
            this._moveSp.addChild(this._bg);
            this._moveBtn = ObjCreate.createGButton("", 0, 0, 24, 22, this, "MoveBtn");
            this._moveBtn.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMoveBtnMouseDown);
            this._moveBtn.toolTipData = HTMLUtil.addColor(Language.getString(60068), "#FFFFFF");
            this._titleSp = new MagicTowerInfoTitle();
            this._titleSp.width = 275;
            this._titleSp.height = 32;
            this._titleSp.x = 24;
            this._titleSp.y = -5;
            this._titleSp.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMoveBtnMouseDown);
            addChild(this._titleSp);
            this._skinSp = new Object /* flash.display.Sprite */();
            this._skinSp.x = 60;
            this._skinSp.y = 72;
            addChild(this._skinSp);
            LoaderHelp.addResCallBack(ResFileConst.heroBattle, this.onLoaderComplete);
            this._skillItemBg = GlobalClass.getBitmap(ImagesConst.PackItemBg);
            this._skillItemBg.y = 65;
            this._skillItemBg.x = 230;
            this._skillItem.setSize(32, 32);
            this._skillItem.y = 68;
            this._skillItem.x = 233;
            this._skillItem.isDragAble = false;
            addChild(this._skillItemBg);
            addChild(this._skillItem);
            var loc1=ObjCreate.createTextField("", 213, 144, 60, 20, this);
            loc1.htmlText = Language.getString(27618);
            loc1.addEventListener(flash.events.TextEvent.LINK, this.onLinkTxtHandler);
            ObjCreate.createTextField(Language.getString(200216), 55, 144, 135, 20, this);
            loc2 = 0;
            while (loc2 < 3) 
            {
                loc4 = new BaseItem();
                this.allItem.push(loc4);
                this._itemBg = GlobalClass.getBitmap(ImagesConst.PackItemBg);
                this.allItem[loc2].setSize(32, 32);
                this.allItem[loc2].move(2, 2);
                this._itemBg.y = 65;
                loc4.doubleClickEnabled = true;
                loc4.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.onDelCompose);
                this._itemBg.x = loc2 * 32 + loc2 * 32 + 20;
                this.allItem[loc2].x = this._itemBg.x + 3;
                this.allItem[loc2].y = this._itemBg.y + 3;
                addChild(this._itemBg);
                addChild(this.allItem[loc2]);
                ++loc2;
            }
            loc3 = 0;
            while (loc3 < 4) 
            {
                loc5 = new BaseItem();
                this.materialItem.push(loc5);
                loc5.addEventListener(flash.events.MouseEvent.CLICK, this.addMaterial);
                this.materialItem[loc3].setSize(32, 32);
                this.materialItem[loc3].move(2, 2);
                this.materialItem[loc3].filters = [FilterConst.colorFilter2];
                this._itemBg = GlobalClass.getBitmap(ImagesConst.PackItemBg);
                this._itemBg.x = loc3 * 32 + loc3 * 32 + 35;
                this._itemBg.y = 30;
                this.materialItem[loc3].x = this._itemBg.x + 3;
                this.materialItem[loc3].y = this._itemBg.y + 3;
                this._moveSp.addChild(this._itemBg);
                this._moveSp.addChild(this.materialItem[loc3]);
                ++loc3;
            }
            this._composeBtn = ObjCreate.createGButton(Language.getString(200213), 227, 110, 43, 20, this);
            this._composeBtn.enabled = false;
            this._composeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onClick);
            this._skillInfo = new SkillInfo();
            return;
        }

        /* internal  */onLoaderComplete(): void
        {
            ObjCreate.createBitmap(ImagesConst.HeroBattleSign, 192, 2, this._moveSp);
            ObjCreate.createBitmap(ImagesConst.HeroBattleAdd, 0, 0, this._skinSp);
            ObjCreate.createBitmap(ImagesConst.HeroBattleAdd, 65, 0, this._skinSp);
            ObjCreate.createBitmap(ImagesConst.HeroBattleEqual, 125, 0, this._skinSp);
            return;
        }

        /* internal  */onLinkTxtHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            if (!this._skillTipsPanel) 
            {
                this._skillTipsPanel = new SkillComposeTipsView();
                this._skillTipsPanel.updateSkill();
            }
            if (this._skillTipsPanel.isHide) 
            {
                this._skillTipsPanel.show(this.x + this.width + 40, this.y - 120);
            }
            else 
            {
                this._skillTipsPanel.hide();
            }
            return;
        }

        protected addMaterial(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=null;
            var loc4=0;
            var loc5=0;
            if (arg1.currentTarget.filters && arg1.currentTarget.filters.length > 0 || (arg1.currentTarget as BaseItem).itemData == null) 
            {
                return;
            }
            loc1 = (arg1.currentTarget as BaseItem).itemData;
            loc2 = loc1.itemCode;
            var loc6=0;
            var loc7=this.allItem;
            for(loc3 of loc7) 
            {
                if (loc3.itemData) 
                {
                    continue;
                }
                loc3.itemData = new ItemData(loc2);
                this.autoSelectMaterial();
                break;
            }
            if (this.isCanCompose()) 
            {
                loc5 = 0;
                while (loc5 < this.materialItem.length) 
                {
                    this.materialItem[loc5].filters = [FilterConst.colorFilter2];
                    ++loc5;
                }
                loc4 = CopyConfig.instance.getHeroBattleFormula(this.getComposeIndex().split("#"));
                this._skillInfo.tSkill = SkillConfig.instance.getInfoByName(loc4);
                this._skillItem.data = this._skillInfo;
                this._skillItem.toolTipData = this._skillInfo.getToolTipInfo();
                this._composeBtn.enabled = true;
            }
            return;
        }

        /* internal */ /* var */_moveRec: Object /* flash.geom.Rectangle */;

        protected /* var */_moveSp: Object /* flash.display.Sprite */;

        protected /* var */composeBtn: GButton;

        protected /* var */_moveBtn: GButton;

        protected /* var */_titleSp: MagicTowerInfoTitle;

        public /* var */allItem: Vector.<BaseItem>;

        public /* var */materialItem: Vector.<BaseItem>;

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_itemBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_composeBtn: GButton;

        /* internal */ /* var */_skillItem: SkillItem;

        /* internal */ /* var */_skillInfo: SkillInfo;

        /* internal */ /* var */_skillItemBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_skinSp: Object /* flash.display.Sprite */;

        /* internal */ /* var */_skillTipsPanel: SkillComposeTipsView;

        /* internal */ /* var */codeArr: Array<any>;
    }
